//
//  DeviceListCell.m
//  Holyiot-DFU
//
//  Created by bill on 2021/2/23.
//

#import "DeviceListCell.h"
#import "BluetoothCenter.h"
@interface DeviceListCell()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *uuidHeight;
@property (weak, nonatomic) IBOutlet UILabel *typeLab;
@property (weak, nonatomic) IBOutlet UILabel *minorTitleLab;
@property (weak, nonatomic) IBOutlet UILabel *majorTitleLab;
@property (weak, nonatomic) IBOutlet UILabel *UUIDTitleLab;


@end
@implementation DeviceListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}
-(void)setPeripheral:(CBPeripheral *)peripheral
{
    _peripheral = peripheral;
    self.uuidStr =  peripheral.identifier.UUIDString;
    NSDictionary *deviceInfo = [[BluetoothCenter shareInstance].deviceInfo objectForKey:peripheral.identifier.UUIDString];
    self.typeLab.text = deviceInfo[device_type];
    BOOL isHide = ![deviceInfo[device_type]isEqualToString:@"Beacon"];
    self.minorTitleLab.hidden = isHide;
    self.majorTitleLab.hidden = isHide;
    self.UUIDTitleLab.hidden = isHide;
    //设备名称
    if (deviceInfo[device_name]) {
        self.nameLab.text = deviceInfo[device_name];
    }else{
        self.nameLab.text = peripheral.name;
    }
    //beacon uuid
    self.uuidLab.text = [deviceInfo objectForKey:device_beacon_uuid];
    //major
    self.majorLab.text = [deviceInfo objectForKey:device_major];
    //minor
    self.minorLab.text = [deviceInfo objectForKey:device_minor];
    //mac地址
    self.macLab.text = [deviceInfo objectForKey:device_mac];
    //信号强度
    self.rssiLab.text = [NSString stringWithFormat:@"%@db",deviceInfo[device_rssi]];
//    NSLog(@"======>%@",deviceInfo);
    //电量
    if ([deviceInfo objectForKey:device_battery]) {
        self.batteryLab.text = [NSString stringWithFormat:@"%@%%",[deviceInfo objectForKey:device_battery]];
    }else{
        self.batteryLab.text = nil;
    }
   
    
    
    
    
    
    if ([deviceInfo[device_rssi] intValue] >= -20) {
        //ic_rssi1
        self.rssi_img.image = [UIImage imageNamed:@"ic_rssi5"];
    }else if ([deviceInfo[device_rssi] intValue]<-20&&[deviceInfo[device_rssi] intValue] >= -40){
        self.rssi_img.image = [UIImage imageNamed:@"ic_rssi4"];
    }else if ([deviceInfo[device_rssi] intValue]<-40&&[deviceInfo[device_rssi] intValue] >= -60){
        self.rssi_img.image = [UIImage imageNamed:@"ic_rssi3"];
    }else if ([deviceInfo[device_rssi] intValue]<-60&&[deviceInfo[device_rssi] intValue] >= -80){
        self.rssi_img.image = [UIImage imageNamed:@"ic_rssi2"];
    }else if ([deviceInfo[device_rssi] intValue]<-80){
        self.rssi_img.image = [UIImage imageNamed:@"ic_rssi1"];
    }
}
- (void)setUuidStr:(NSString *)uuidStr
{
    NSMutableParagraphStyle*style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineBreakMode =NSLineBreakByCharWrapping;
    style.alignment =NSTextAlignmentLeft;
    CGFloat w =  _uuidLab.bounds.size.width;
    CGFloat h =   [uuidStr boundingRectWithSize:CGSizeMake(w, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17],NSParagraphStyleAttributeName:style} context:nil].size.height;
   
   _uuidHeight.constant = h+1;
//    NSLog(@"=%@=",uuidStr);
//    NSLog(@"计算出来的宽度:%f,高度:%f",w,h);
    _uuidLab.text = uuidStr;
}
-(void)refreshUI
{
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}


@end
